package com.example.administrator.suspension_window.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;

import com.example.administrator.suspension_window.R;


public class TableShowView extends View {


    Context						c;
    WindowManager				mWM;		// WindowManager
    WindowManager.LayoutParams	mWMParams;	// WindowManager参数
    View						win;
    int                         type;

    public TableShowView(Context context) {
        // TODO Auto-generated constructor stub
        super(context);
        c = context;
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    public void fun() {
        // 设置载入view WindowManager参数
        mWM = (WindowManager) c.getSystemService(Context.WINDOW_SERVICE);
        win = LayoutInflater.from(c).inflate(R.layout.layout_widget,null);
        win.setBackgroundColor(getResources().getColor(R.color.colorPrimary));

        ImageView vwImage = win.getRootView().findViewById(R.id.imageView);
        vwImage.setImageDrawable(getResources().getDrawable(R.mipmap.ic_launcher));



        WindowManager wm = mWM;
        final WindowManager.LayoutParams wmParams = new WindowManager.LayoutParams();
        mWMParams = wmParams;
        wmParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        wmParams.flags = 40;// 这句设置桌面可控
        wmParams.width = 600;
        wmParams.height = 600;
        wmParams.format = -3; // 透明

        // 这里是随便载入的一个布局文件


        win.setOnTouchListener(new OnTouchListener() {
            // 触屏监听
            int	lastX, lastY;
            int secondX,secondY;

            @SuppressLint("ClickableViewAccessibility")
            public boolean onTouch(View v, MotionEvent event) {
                final int action = event.getAction();
                float x = event.getX();
                float y = event.getY();
                switch (action){
                    case MotionEvent.ACTION_DOWN:
                        lastX = (int)x;
                        lastY = (int)y;
                        type = 0;
                        break;
                    case MotionEvent.ACTION_POINTER_DOWN:
                        secondX = (int)x;
                        secondY = (int)y;
                        type = 1;
                        break;
                    case MotionEvent.ACTION_MOVE:
                        if(type == 0){
                            mWMParams.x += (x - lastX)/2; // 偏移量
                            mWMParams.y += (y - lastY)/2; // 偏移量
                            mWM.updateViewLayout(win, mWMParams);
                        }else if(type == 1){
                            wmParams.width = 700;
                            wmParams.height = 700;
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                        break;
                }

                return true;
            }
        });







        wm.addView(win, wmParams);// 这句是重点 给WindowManager中丢入刚才设置的值
        // 只有addview后才能显示到页面上去。
        // 注册到WindowManager win是要刚才随便载入的layout，wmParams是刚才设置的WindowManager参数集
        // 效果是将win注册到WindowManager中并且它的参数是wmParams中设置饿

    }
}
